package com.piz.web.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.piz.model.common.NavItem;
import com.piz.service.common.NavSetUpService;
 
public class InitialSetupInterceptor extends HandlerInterceptorAdapter{
 
	@Autowired
    NavSetUpService navSetUpService;
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		String loginname = SecurityContextHolder.getContext().getAuthentication().getName();
		if(loginname.equalsIgnoreCase("anonymousUser"))
		{
			loginname = "";
		}
		List<NavItem> nav =  navSetUpService.fetchTopNavItemsByUsername(loginname);
		List<NavItem> leftNav =  navSetUpService.fetchLeftNavItemsByUsername(loginname);
		modelAndView.getModelMap().put("nav", nav);
		modelAndView.getModelMap().put("leftNav", leftNav);
		modelAndView.getModelMap().put("loginname", loginname);
		super.postHandle(request, response, handler, modelAndView);
	}
	//before the actual handler will be executed
	public boolean preHandle(HttpServletRequest request, 
			HttpServletResponse response, Object handler)
	    throws Exception {		
		
		return true; 
	}
}
